package com.lgj.web.sys.controller;

import com.github.pagehelper.PageInfo;
import com.lgj.result.ResultBody;
import com.lgj.web.sys.model.SysMenu;
import com.lgj.web.sys.model.SysResource;
import com.lgj.web.sys.model.SysRole;
import com.lgj.web.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/role")
public class SysRoleController {
    @Autowired
    private SysRoleService roleService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody create(@RequestBody SysRole role) {
        int count = roleService.create(role);
        if (count > 0) {
            return ResultBody.success(count);
        }
        return ResultBody.fail();
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody update(@PathVariable Long id, @RequestBody SysRole role) {
        int count = roleService.update(id, role);
        if (count > 0) {
            return ResultBody.success(count);
        }
        return ResultBody.fail();
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody delete(@RequestParam("ids") List<Long> ids) {
        int count = roleService.delete(ids);
        if (count > 0) {
            return ResultBody.success(count);
        }
        return ResultBody.fail();
    }

    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<List<SysRole>> listAll() {
        List<SysRole> roleList = roleService.list();
        return ResultBody.success(roleList);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<PageInfo> list(@RequestParam(value = "keyword", required = false) String keyword,
                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        return ResultBody.success(roleService.list(keyword, pageSize, pageNum));
    }

    @RequestMapping(value = "/updateStatus/{id}", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody updateStatus(@PathVariable Long id, @RequestParam(value = "status") Integer status) {
        SysRole SysRole = new SysRole();
        SysRole.setStatus(status);
        int count = roleService.update(id, SysRole);
        if (count > 0) {
            return ResultBody.success(count);
        }
        return ResultBody.fail();
    }

    @RequestMapping(value = "/listMenu/{roleId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<List<SysMenu>> listMenu(@PathVariable Long roleId) {
        List<SysMenu> roleList = roleService.listMenu(roleId);
        return ResultBody.success(roleList);
    }

    @RequestMapping(value = "/listResource/{roleId}", method = RequestMethod.GET)
    @ResponseBody
    public ResultBody<List<SysResource>> listResource(@PathVariable Long roleId) {
        List<SysResource> roleList = roleService.listResource(roleId);
        return ResultBody.success(roleList);
    }

    @RequestMapping(value = "/allocMenu", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody allocMenu(@RequestParam Long roleId, @RequestParam List<Long> menuIds) {
        int count = roleService.allocMenu(roleId, menuIds);
        return ResultBody.success(count);
    }

    @RequestMapping(value = "/allocResource", method = RequestMethod.POST)
    @ResponseBody
    public ResultBody allocResource(@RequestParam Long roleId, @RequestParam List<Long> resourceIds) {
        int count = roleService.allocResource(roleId, resourceIds);
        return ResultBody.success(count);
    }

}
